package com.workcell.workdata.im.component.function;

import com.farsunset.cim.model.Message;
import com.workcell.workdata.im.constant.MessageAction;
import com.workcell.workdata.im.constant.MessageFormat;
import com.workcell.workdata.im.model.dto.MessageTitle;
import com.workcell.workdata.im.model.message.*;
import com.workcell.workdata.im.service.GroupService;
import com.workcell.workdata.im.service.MicroServerService;
import com.workcell.workdata.im.service.UserService;
import com.workcell.workdata.im.util.JSON;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
public class APNsAlertHelper {

    @Resource
    private GroupService groupService;

    @Resource
    private UserService userService;

    @Resource
    private MicroServerService microServerService;

    public String getTitle(Message message){

        String action = message.getAction();

        if (action.equals(MessageAction.ACTION_3) || action.equals(MessageAction.ACTION_4)){
            return groupService.getName(Long.parseLong(message.getSender()));
        }

        if (action.equals(MessageAction.ACTION_200)){
            return microServerService.getName(Long.parseLong(message.getSender()));
        }

        if (action.equals(MessageAction.ACTION_0)
                || action.equals(MessageAction.ACTION_900)
                || action.equals(MessageAction.ACTION_901)
                || action.equals(MessageAction.ACTION_910)
                || action.equals(MessageAction.ACTION_911)){
            return userService.findName(Long.parseLong(message.getSender()));
        }

        return "系统";
    }

    public String getBody(Message message){
        String action = message.getAction();

        if (action.equals(MessageAction.ACTION_900) || action.equals(MessageAction.ACTION_901)){
            return "请求与您通话";
        }

        if (action.equals(MessageAction.ACTION_910) || action.equals(MessageAction.ACTION_911)){
            return "邀请您加入语音会议";
        }

        if (action.equals(MessageAction.ACTION_3) || action.equals(MessageAction.ACTION_4)){
            MessageTitle title = JSON.parse(message.getTitle(),MessageTitle.class);
            boolean isText = String.valueOf(MessageFormat.TEXT).equals(message.getFormat());
            return title.getName() + (isText ? ":" : "") + getMessageText(message);
        }

        return getMessageText(message);
    }

    private String getMessageText(Message message){
        byte format = Byte.parseByte(message.getFormat());
        if (MessageFormat.IMAGE == format) {
            return "发来一张图片";
        }

        if (MessageFormat.VOICE == format) {
            return "发来一段语音";
        }

        if (MessageFormat.VIDEO == format) {
            return "发来一个短视频";
        }

        if (MessageFormat.FILE == format) {
            ChatFile file = JSON.parse(message.getContent(),ChatFile.class);
            return "发来文件:"+file.getName();
        }

        if (MessageFormat.MAP == format) {
            ChatMap map = JSON.parse(message.getContent(), ChatMap.class);
            return "发来地图:" + map.getAddress();
        }

        if (MessageFormat.EMOTICON == format) {
            ChatEmoticon emoticon = JSON.parse(message.getContent(), ChatEmoticon.class);
            return "发来表情:[" + emoticon.getName()+"]";
        }

        if (MessageFormat.PROFILE_CARD == format || MessageFormat.APP_PROFILE_CARD == format) {
            ChatProfile profile = JSON.parse(message.getContent(), ChatProfile.class);
            return "发来名片:[" + profile.getName()+"]";
        }

        if (MessageFormat.LINK == format) {
            ChatLink link = JSON.parse(message.getContent(), ChatLink.class);
            return "发来链接:" + link.getTitle();
        }

        if (MessageFormat.LINK_LIST== format) {
            ChatLinkList link = JSON.parse(message.getContent(), ChatLinkList.class);
            return "发来链接:" + link.getTitle();
        }

        return message.getContent();
    }
}
